package org.jeecg.config.mybatis.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = { "org.jeecg.metasyn.mapper" }, sqlSessionTemplateRef = "metaSqlSessionTemplate")
public class MetaSynSourceConfig {

    @Bean(name = "metaDataSource")
    // @ConfigurationProperties(prefix = "second.datasource")
    public DataSource metaDataSource(@Value("${spring.datasource.dynamic.datasource.multi-datasource1.url}") String url,
                                     @Value("${spring.datasource.dynamic.datasource.multi-datasource1.driver-class-name}") String driver,
                                     @Value("${spring.datasource.dynamic.datasource.multi-datasource1.username}") String userName,
                                     @Value("${spring.datasource.dynamic.datasource.multi-datasource1.password}") String password) {
        DruidDataSource dataSource = new DruidDataSource();
        /* 数据源主要配置 */
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "metaSqlSessionFactory")
    public SqlSessionFactory metaSqlSessionFactory(@Qualifier("metaDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // 添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources("classpath*:org/jeecg/metasyn/mapper/mapping/*.xml"));
            bean.setPlugins(new Interceptor[]{
                    new PaginationInterceptor()
            });
            return bean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Bean(name = "metaSqlSessionTemplate")
    public SqlSessionTemplate metaSqlSessionTemplate(
            @Qualifier("metaSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
        return template;
    }

}
